/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hos_ahmedadeltito.photoeditorsdk.provider;

import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.utils.TextAlignment;
import ohos.app.Context;
import ohos.media.image.PixelMap;

import com.hos_ahmedadeltito.photoeditorsdk.ResourceTable;
import com.hos_ahmedadeltito.photoeditorsdk.utils.DeviceUtil;

import java.util.List;

public class ImageAdapter extends BaseItemProvider {

    private Context mContext;
    private List<PixelMap> list;
    private final int LINE_COUNT = 3;
    private int mItemWidth;
    private ImageClickedListener mClickedListener;

    public void setClickedListener(ImageClickedListener mClickedListener) {
        this.mClickedListener = mClickedListener;
    }

    public ImageAdapter(Context context, List list) {
        this.mContext = context;
        this.list = list;
        mItemWidth = DeviceUtil.getScreenWidth(mContext) / LINE_COUNT;
    }

    @Override
    public int getCount() {
        int needLines = list.size() / LINE_COUNT;
        if (list.size() % LINE_COUNT > 0)
            needLines++;
        return needLines;
    }

    @Override
    public Object getItem(int i) {
        int startIndex = i * LINE_COUNT;
        int endIndex = (i + 1) * LINE_COUNT;
        if (endIndex > list.size())
            endIndex = list.size();
        //此处返回一行的数据列表
        return list.subList(startIndex, endIndex);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        if (component == null) {
            component = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_provider_test_grid_item,
                null, false);
        }
        //每一行的容器
        DirectionalLayout lineLayout = (DirectionalLayout) component.findComponentById(ResourceTable.Id_layout_line_container);
        //移除所有子控件
        lineLayout.removeAllComponents();
        //获取一行的数据
        List<PixelMap> lineItems = (List<PixelMap>) getItem(i);
        //添加每一行的控件
        for (PixelMap imageId : lineItems) {
            Image image = new Image(mContext);
            image.setClipAlignment(TextAlignment.CENTER);
            image.setScaleMode(Image.ScaleMode.ZOOM_CENTER);
            image.setPixelMap(imageId);
            image.setMarginsLeftAndRight(24, 24);
            image.setMarginsTopAndBottom(24, 24);
            DirectionalLayout.LayoutConfig itemLayoutConfig = new DirectionalLayout.LayoutConfig(mItemWidth, mItemWidth * 2 / 3);
            image.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    mClickedListener.onClick(component, imageId);
                }
            });
            lineLayout.addComponent(image, itemLayoutConfig);
        }
        return component;
    }
}
